/*
 * Copyright (C) 2011-2014 GUIGUI Simon, fyhertz@gmail.com
 *
 * This file is part of libstreaming (https://github.com/fyhertz/libstreaming)
 *
 * Spydroid is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This source code is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this source code; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package app.com.rtsplibrary.media;

import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.util.concurrent.TimeUnit;

import android.annotation.SuppressLint;
import android.media.MediaFormat;
import android.util.Log;

import app.com.rtsplibrary.data.ADataUtil;
import app.com.rtsplibrary.data.H264Data;
import app.com.rtsplibrary.rtsp.RtspServer;
import app.com.rtsplibrary.util.T;


/**
 * An InputStream that uses data from a MediaCodec.
 * The purpose of this class is to interface existing RTP packetizers of
 * libstreaming with the new MediaCodec API. This class is not thread safe !
 */
@SuppressLint("NewApi")
public class MediaCodecInputStream extends InputStream {

	public final String TAG = "MediaCodecInputStream";

	private long ts = 0;
	private ByteBuffer mBuffer = null;
	private H264Data data = null;
	private ADataUtil inData = null;


	public MediaFormat mMediaFormat;

	public MediaCodecInputStream() {
		inData = new ADataUtil();
		Log.d(TAG, "MediaCodecInputStream OPEN");
	}

	@Override
	public void close() throws IOException {
		if(inData != null){
			inData.close();
			inData = null;
		}
		super.close();
		Log.d(TAG, "MediaCodecInputStream CLOSE");
	}


	@Override
	public int read() throws IOException {
		return 0;
	}

	@SuppressLint("WrongConstant")
	@Override
	public int read(byte[] buffer, int offset, int length) throws IOException {
		int min = 0;

		if(inData == null){
			return min;
		}

		try{
			if(mBuffer == null){
				do{
					data = inData.getPcmData().poll(1000, TimeUnit.MICROSECONDS);
				}while(inData != null && data == null);

				if(data == null || inData == null) return 0;
				ts = data.ts;
				mBuffer = ByteBuffer.wrap(data.data);
				mBuffer.position(0);
			}
			min = length < data.data.length - mBuffer.position() ? length : data.data.length - mBuffer.position();
			mBuffer.get(buffer, offset, min);
			if (mBuffer.position()>=data.data.length) {
				mBuffer = null;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return min;
	}

	public int available() {
		if (mBuffer != null) {
			return data.data.length - mBuffer.position();
		}else {
			return 0;
		}
	}

	public long getLastts(){
		return ts;
	}
}
